﻿using System;
using System.Collections;
using System.Collections.Generic;
using LogikBug.Collections;

namespace LogikBug.ModularMvc
{
	public class ModuleProviderCollection : IEnumerable<IModuleProvider>
	{
		#region Fields

		private readonly Hashtable<Type, IModuleProvider> _collection;

		#endregion

		#region Constructors

		public ModuleProviderCollection()
		{
			_collection = new Hashtable<Type, IModuleProvider>();
		}

		#endregion

		#region Properties

		public IModuleProvider this[Type key]
		{
			get
			{
				return _collection[key].ThrowIfNull();
			}
		}

		public IReadOnly<IModuleProvider> Providers
		{
			get
			{
				return _collection.Values;
			}
		}

		#endregion

		#region Methods

		public bool Has(Type providerType)
		{
			return _collection.Has(providerType);
		}

		public void Register(IModuleProvider provider)
		{
			var key = provider.GetType();

			if (!_collection.Has(key))
			{
				_collection[key] = provider;
			}
		}

		public void Remove(Type key)
		{
			_collection.Remove(key);
		}

		public IModuleProvider GetValueOrDefault(Type key)
		{
			return _collection.GetValueOrDefault(key);
		}

		#region Implementation of IEnumerable

		public IEnumerator<IModuleProvider> GetEnumerator()
		{
			return _collection.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		#endregion

		#endregion
	}
}
